package com.catchdays.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.catchdays.db.model.CatchDaysModel;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	private static final String TAG = "DatabaseHelper";
	private static final String DATABASE_NAME = "CatchDays";
	private static final int DATABASE_VERSION = 12;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, CatchDaysModel.class);
		} catch (SQLException e) {
			Log.e(TAG, "DB 생성 실패", e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, CatchDaysModel.class, true);
			TableUtils.createTable(connectionSource, CatchDaysModel.class);

			List<CatchDaysModel> dummyList = getCatchDaysDummyList();

			Dao<CatchDaysModel, Integer> catchDaysDao = getDao(CatchDaysModel.class);

			for (CatchDaysModel dummy : dummyList) {
				catchDaysDao.create(dummy);
			}
		} catch (SQLException e) {
			Log.e(TAG, "DB 생성 실패", e);
		}
	}

	public List<CatchDaysModel> getCatchDaysDummyList() {
		List<CatchDaysModel> dummyList = new ArrayList<CatchDaysModel>();

		CatchDaysModel model = new CatchDaysModel();
		model.setAnniversaryYear(1983);
		model.setAnniversaryMonth(1);
		model.setAnniversaryDay(18);
		model.setAnniversaryType(1);
		model.setFacebookId("shpark118@naver.com");
		model.setFavoriteYn(1);
		model.setLunarYn(1);
		model.setMemo("키키");
		model.setModDate(new Date());
		model.setRegDate(new Date());
		model.setTitle("박성훈");
		dummyList.add(model);

		CatchDaysModel model2 = new CatchDaysModel();
		model2.setAnniversaryYear(1982);
		model2.setAnniversaryMonth(10);
		model2.setAnniversaryDay(12);
		model2.setAnniversaryType(1);
		model2.setFacebookId("shpark118@naver.com");
		model2.setFavoriteYn(1);
		model2.setLunarYn(1);
		model2.setMemo("키키");
		model2.setModDate(new Date());
		model2.setRegDate(new Date());
		model2.setTitle("천선영");
		dummyList.add(model2);

		CatchDaysModel model3 = new CatchDaysModel();
		model3.setAnniversaryYear(2013);
		model3.setAnniversaryMonth(9);
		model3.setAnniversaryDay(18);
		model3.setAnniversaryType(1);
		model3.setFacebookId("shpark118@naver.com");
		model3.setFavoriteYn(1);
		model3.setLunarYn(1);
		model3.setMemo("사랑합니다");
		model3.setModDate(new Date());
		model3.setRegDate(new Date());
		model3.setTitle("박알콩");
		dummyList.add(model3);

		return dummyList;
	}
}
